﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight.Chart;
using IndexMarkSilverlightClient.IndexMarkServiceReference;
using SharedCode;
using Section = IndexMarkSilverlightClient.IndexMarkServiceReference.Section;


namespace IndexMarkSilverlightClient.Views.Controls.ReportsControls
{
    public partial class LIDIA : UserControl
    {
        private readonly IndexMarkServiceReference.IndexMarkServiceClient client = new IndexMarkServiceClient();
       // private ObservableCollection<IndexRow> _res=new ObservableCollection<IndexRow>();
        private int IDTerm;
        private ObservableCollection<Section> _sc = new ObservableCollection<Section>();

        public IndexRow IR;

        public LIDIA()
        {
            InitializeComponent();            
            Loaded += new RoutedEventHandler(LIDIA_Loaded);
        }

        public LIDIA(IndexRow row, int trm)
        {
            InitializeComponent();
            IDTerm = trm;
            IR = row;
            Loaded += new RoutedEventHandler(LIDIA_Loaded);
            DrawLidia();
        }

        void LIDIA_Loaded(object sender, RoutedEventArgs e)
        {

            //(((((((this.Parent as Grid).Parent as Grid).Parent as ReportsMain).Parent as Frame).Parent as Border).Parent as Grid).Parent as MainPage).ViewModel.SelectTerm -= ViewModel_SelectTerm;
            //(((((((this.Parent as Grid).Parent as Grid).Parent as ReportsMain).Parent as Frame).Parent as Border).Parent as Grid).Parent as MainPage).ViewModel.SelectTerm += new MainViewModel.SelectTermEventHandler(ViewModel_SelectTerm);
            //(((((((this.Parent as Grid).Parent as Grid).Parent as ReportsMain).Parent as Frame).Parent as Border).Parent as Grid).Parent as MainPage).ViewModel.SelectTermInit();
            //client.GetLIDIACompleted += new EventHandler<GetLIDIACompletedEventArgs>(client_GetLIDIACompleted);
            client.GetParentsSectionsCompleted += new EventHandler<GetParentsSectionsCompletedEventArgs>(client_GetParentsSectionsCompleted);
            client.GetParentsSectionsAsync(IDTerm);
            //client.GetLIDIAAsync(IDTerm, new ObservableCollection<int> { 3172 });
            
        }

        void client_GetParentsSectionsCompleted(object sender, GetParentsSectionsCompletedEventArgs e)
        {
            _sc = e.Result;
            SectionLB.ItemsSource = e.Result;
            //SectionLB.SelectionMode = SelectionMode.Multiple;
           // SectionLB.SelectAll();
        }

        void client_GetLIDIACompleted(object sender, GetLIDIACompletedEventArgs e)
        {
            //_res = e.Result;
            DrawLidia();
        }


        void ViewModel_SelectTerm(object sender, Classes.SelectTermEventsArgs e)
        {
             IDTerm = e.SelectTerm.IDTerm;
        }


        //void DrawLidia()
        //{
        //    string val_name = "";
        //    DoubleCollection dc = new DoubleCollection();
        //    foreach (IndexRow LI in _res)
        //    {
        //        LI.SectionIndex.OrderBy(x => x.Index);
        //        foreach(var tmp in LI.SectionIndex )
        //        {
        //            val_name += tmp.Section + " ";
        //            dc.Add(tmp.Index);   
        //        }
                   

        //    }

        //    SolidColorBrush cb1 = new SolidColorBrush(Color.FromArgb(100, 102, 102, 150));
        //    Brush[] mb = new Brush[] { cb1 };
        //    DataSeries ds1 = new DataSeries();
        //    ds1.Values = dc;

        //    ds1.Label = "Респондент такой-то период ";
        //    LIDIAChart.Data.Children.Clear();
        //    LIDIAChart.Data.ItemNames = val_name;
        //    LIDIAChart.CustomPalette = mb;
        //    LIDIAChart.Data.Children.Add(ds1);

        //}


        void DrawLidia()
        {
            string val_name = "";
            DoubleCollection dc = new DoubleCollection();

            var SI = new ObservableCollection2<SectionIndex>(IR.SectionIndex.OrderBy(x => x.Index).Reverse()); 
            foreach (var tmp in SI)
            {
                //var v = _sc.Where(x => x.Order == tmp.Section);
                //if (!v.Any() || v.First().Check)
                {
                    val_name += "P" + tmp.Section + " ";
                    dc.Add(tmp.Index);
                }
            }

            SolidColorBrush cb1 = new SolidColorBrush(Color.FromArgb(100, 102, 102, 150));
            
            Brush[] mb = new Brush[] { cb1 };
            DataSeries ds1 = new DataSeries();
            ds1.Values = dc;

            ds1.Label = IR.Resp.Person.ToString() + ", " + IR.Position;
            LIDIAChart.Data.Children.Clear();
            LIDIAChart.Data.ItemNames = val_name;
            LIDIAChart.CustomPalette = mb;
            LIDIAChart.Data.Children.Add(ds1);

        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            
          
        }

        private void SectionLB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DrawLidia();
        }
    }
}
